#include "nlohmann/json.hpp"
#include <string>
#include <iostream>
#include "httplib.h"
#include "cmdline/cmdline.h"
using namespace std;
using json = nlohmann::json;
static cmdline::parser s_cmdParse;

string project_id="1063";  //#hub项目id 
string product="711";
string build="1063";
string g_versionID="";

int opntionParam()
{
  //产品id
  s_cmdParse.add<string,function<string(string)>>("product",0,"zentao product id",false,"",[=](const  string s){
         product = s;
         return "";
    });
  s_cmdParse.add<string,function<string(string)>>("project",0,"zentao product id",false,"",[=](const  string s){
         project_id = s;
         return "";
    });
  s_cmdParse.add<string,function<string(string)>>("buildID",0,"zentao project buildID",false,"",[=](const  string s){
         build = s;
         return "";
    });
  s_cmdParse.add<string,function<string(string)>>("versionID",0,"zentao project versionID",false,"",[=](const  string s){
        g_versionID = s;
         return "";
    });
}
int main(int argc,char*argv[])
{
    opntionParam();
    s_cmdParse.parse_check(argc,argv);
    //using namespace nlohmann::literals;
    string sessionID="";
    string username="shixiongjie";
    string password="shixiongjie";
    string token="";

    string versionName="";
    string versionID="";
    string versionDate="";
    //std::cout<<t;
    httplib::Headers H;
    H.emplace(pair<string,string>("Content-Type","application/json"));
    httplib::Client cli("http://git.roombanker.cn:801");
    if (auto res = cli.Get("/zentao/api-getSessionID.json",H)) { 
        //cout << res->body << endl;
        auto sessionJson = json::parse(res->body);
        //cout<<sessionJson["data"]<<endl;
        string tmp = sessionJson["data"];
        auto datajson = json::parse(tmp);
        sessionID = datajson["sessionID"];
        std::cout<<"sessionID:"<<sessionID<<endl;
    } else {
      cout << "error code: " << res.error() << std::endl;
      return 0;
  }
  //登录，获取token
  string LOGIN_PARAMS="account="+username+"&password="+password;
  if (auto res = cli.Post("/zentao/user-login-"+sessionID+".json?"+LOGIN_PARAMS)) { 
        //cout << res->status << endl;
        //cout << res->get_header_value("Content-Type") << endl;
        //cout << res->body << endl;
        json tokenJson = json::parse(res->body);
        token = tokenJson["user"]["token"];
        std::cout<<"token:"<<token<<endl;
    } else {
    cout << "error code: " << res.error() << std::endl;
    return 0;
  }

  //获取版本信息
  ///zentao/project-build-1063.json?zentaosid=
  if (auto res = cli.Post("/zentao/project-build-"+build+".json?zentaosid="+token)) { 
        //    cout << res->body << endl;
        json tokenJson = json::parse(res->body);
        string strdata = tokenJson["data"];
        json dataJson = json::parse(strdata);

        versionName = dataJson["projectBuilds"][product][0]["name"];
        versionID = dataJson["projectBuilds"][product][0]["id"];
        versionDate = dataJson["projectBuilds"][product][0]["date"];
        std::cout<<"lastest version:"<<versionID;
        std::cout<<",lastest versionName:"<<versionName<<endl;
    } else {
    cout << "error code: " << res.error() << std::endl;
    return 0;
  }
  cout<<"product:"<<product<<",project:"<<project_id<<endl;
  //获取附件列表
  ///zentao/doc-showFiles-project-1063.json?zentaosid=
if (auto res = cli.Post("/zentao/doc-showFiles-project-"+build+".json?zentaosid="+token)) { 
       // cout << res->status << endl;
       // cout << res->get_header_value("Content-Type") << endl;
       // cout << res->body << endl;
        json infoJson = json::parse(res->body);
        string strdata = infoJson["data"];
        json dataJson = json::parse(strdata);
        if(g_versionID!="")
        {
          versionID = g_versionID;
          cout<<"get current verion:"<<versionID<<endl;
        }
        for(auto &it:dataJson["files"].items())
        {
          //5950
          if(it.value()["objectID"]==versionID)
          {
            string url = it.value()["webPath"];
            string filename=it.value()["title"];
            cout<<filename<<":"<<"http://git.roombanker.cn:801"+url<<endl;
            FILE*fp = fopen(filename.c_str(),"wb");
            if(fp==NULL)
            {
              continue;
            }
            if(auto res = cli.Get(url,[&](const char *data, size_t data_length){
                fwrite(data,data_length,1,fp);
                return true;
            }))
            {
              fclose(fp);
            }
          }

        }
    } else {
    cout << "error code: " << res.error() << std::endl;
    return 0;
  }

    return 0;
}

